package com.sales.service.thread;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import com.sales.service.http.HttpServiceHelper;
import com.sales.service.http.ServiceRequest;
import com.sales.service.queue.ServiceQueueManager;

import android.util.Log;

/**
 * This ServiceThread class is the thread class. It implements the Runnable
 * class. The threads are declared and started in the constructor. The run()
 * method is defined. Once the thread is started the codes in the run method
 * gets executed.
 * 
 * @author Sega
 * 
 */
public class ServiceThread implements Runnable {

	/**
	 * Strings for debugging process by log.i method
	 */
	private static final String TagProject = "SalesService";
	private static final String TagPage = "Thread ::";

	/**
	 * The Thread and the id of the thread is declared.
	 */
	private Thread th;
	private int threadId;

	/**
	 * This is the constructor of the runnable implemented class. Only here the
	 * threads are initialised and started.
	 * 
	 * @param threadId
	 *            - received the id of the thread.
	 */
	public ServiceThread(int threadId) {
		this.threadId = threadId;
	}

	/**
	 * This start() method is used to start the thread.
	 */
	public void start() {
		th = new Thread(this);
		th.start();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 * 
	 * This is the implemented method of the Runnable class. The codes written
	 * in this method are executed when the thread is started.
	 */
	@Override
	public synchronized void run() {
		ServiceRequest runResult = ServiceQueueManager.getInstance().queuePoll();
		Log.i(TagProject, TagPage + "Run Method - threadId: " + threadId + ", result: " + runResult);
		if (runResult != null) {
			Log.i(TagProject, TagPage + "not null- result: " + runResult.getRequesturl());
			HttpServiceHelper helper = new HttpServiceHelper(runResult);
			HttpEntity entity=null;
			try {
				entity = helper.execute();
			} catch (ClientProtocolException e) {
				Log.i(TagProject, TagPage + "run: 11 " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
				e.printStackTrace();
			} catch (IOException e) {
				Log.i(TagProject, TagPage + "run: 12 " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
				e.printStackTrace();
			} catch (URISyntaxException e) {
				Log.i(TagProject, TagPage + "run: 13 " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
				e.printStackTrace();
			}
			InputStream inputStream = null;
			try {
				inputStream = entity.getContent();
			} catch (IllegalStateException e) {
				Log.i(TagProject, TagPage + "run: 21 " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
				e.printStackTrace();
			} catch (IOException e) {
				Log.i(TagProject, TagPage + "run: 22 " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
				e.printStackTrace();
			} catch (Exception e) {
				Log.i(TagProject, TagPage + "run: 23 " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
				e.printStackTrace();
			}
			try {
				runResult.getResponseListener().executeResponse(inputStream);
			} catch (JSONException e) {
				Log.i(TagProject, TagPage + "run: 31 " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
				e.printStackTrace();
			} catch (IOException e) {
				Log.i(TagProject, TagPage + "run: 32 " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
				e.printStackTrace();
			}
		}
	}

}